﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimoBotRewritten
{
    public partial class frmMain : Form
    {
        public Engine SimoEngine;

        delegate void SetTextCallback(string s);
        string channel;

        public frmMain(Engine SageEngine)
        {
            SimoEngine = SageEngine;
            InitializeComponent();
            SimoEngine.MessageReceived += new MessageEventHandler(OnMessageEvent);
        }

        private void btnShowConnectForm_Click(object sender, EventArgs e)
        {
            frmConnect ConnectForm = new frmConnect(this);
            ConnectForm.ShowDialog();
        }

        public string GetMsgBoxTxt(int IndexOfMsg) // Gets the message at the wanted index.
        {
            string MsgBoxTxt = lsbMsgBox.Items[IndexOfMsg].ToString();
            return MsgBoxTxt;
        }

        private void btnConnect_Click_1(object sender, EventArgs e)     //It's pretty stupid to do it this way. But my time is limited and this is by far the fastest option
        {
            string server = lsbMsgBox.Items[0].ToString();
            int port = Convert.ToInt32(lsbMsgBox.Items[1].ToString());
            channel = lsbMsgBox.Items[2].ToString();
            string nick = lsbMsgBox.Items[3].ToString();

            SimoEngine.RunIrc(server, port, channel, nick);
        }

        public void OnMessageEvent(object sender, MessageEventArgs e)
        {
            SetText(e.Message);
        }

        public void SetText(string msg)
        {
            // InvokeRequired is set to true if you are accessing the control from a different thread.
            // In that case, you need to invoke your function using a delegate.
            if (lsbMsgBox.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                lsbMsgBox.Invoke(d, new object[] { msg });
                // Basically this runs the same function again, but now the run order comes from the same thread, so InvokeRequired
                // is set to false, and it goes straight to the else below.
            }
            else
            {
                lsbMsgBox.Items.Add(msg); // Just add the text to the listbox.
                lsbMsgBox.TopIndex = lsbMsgBox.Items.Count - 1; // Scroll the listbox to the lowest item. ...Count -1 because indexes start from 0 and the number of items from 1.
            }
        }

        public void SetConnectInfo(string server, string port, string channel, string nick)
        {
            lsbMsgBox.Items.Clear();
            lsbMsgBox.Items.Add(server);
            lsbMsgBox.Items.Add(port);
            lsbMsgBox.Items.Add(channel);
            lsbMsgBox.Items.Add(nick);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
                SimoEngine.ircWriter.WriteLine("QUIT :Shutting down in order to add more obscure features!");
                SimoEngine.tcpClient.Close();
                SimoEngine.networkStream.Close();
                SimoEngine.ircReader.Close();

                Application.Exit();
        }
    }
}
